package com.kingsoft.dc.khaos.plugin.core.utils;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;

import java.io.IOException;

public class SecurityUtils {

    // Secure Hadoop proxy user params
    public static final String ENABLE_PROXYING = "dts.should.proxy"; // boolean
    public static final String PROXY_KEYTAB_LOCATION = "proxy.keytab.location";
    public static final String PROXY_USER = "proxy.user";
    public static final String OBTAIN_BINARY_TOKEN = "obtain.binary.token";
    public static final String PROXY_KRB5_CONF = "proxy.krb5.conf";
    private static UserGroupInformation loginUser = null;
    public static final String RESOURCE_CONF_DIR = "resource.conf.dir";

    /**
     * Create a proxied user based on the explicit user name, taking other parameters necessary from
     * properties file.
     */
    public static void hadoopKerberosAuth(final String proxyUser, final String proxyKrb5Conf, final String proxyKeytabLocation)
            throws IOException {

        Configuration conf = new Configuration();
        System.setProperty("java.security.krb5.conf", proxyKrb5Conf);

        if (conf == null) {
            throw new IllegalArgumentException("conf can't be null");
        }
        conf.set("hadoop.security.authentication", "kerberos");
        UserGroupInformation.setConfiguration(conf);
        UserGroupInformation.loginUserFromKeytab(proxyUser, proxyKeytabLocation);
    }

}
